#!/bin/bash
# Lailin, June. 2012
# to copy the skimmed dataset back to local group disk, using dq2-get

# dq2 set up
source /afs/atlas.umich.edu/home/lailinxu/Scripts/Grid/grid_setup.sh

if [ $# -lt 1 ]; then 
    echo "usage: ./get_output.sh arg1"
    exit 0
fi

Output="Output"
if [ ! -d "$Output" ]; then mkdir "$Output"; fi

input_list_log=$1
name=`basename $1`
output_log=dq2_$name

while read jobID output Njobs; do
    if [ "Njobs" == "FAIL" ]; then
        echo "Warning => Failed jobs: $output"
        continue
    fi
    dq2=`dq2-ls -r -c "$output/"`
    job_status=`echo $dq2 | grep COMPLETE`
    if [ "$job_status" != "" ]; then
        outdir=`echo $dq2 | grep lxu | cut -f1 -d":"`     
        if [ ! -d "$Output/$outdir" ]; then
            cd $Output
            dq2-get "$output/" > temp_mc
            head -n 1 temp_mc >> $output_log
            tail -n 9 temp_mc >> $output_log
            cd ../
        fi
    fi
done < $input_list_log
